// export const HTTP = "https://same.ascetic.top/index.php?i=666&m=longbing_massages_city&s=";
export const HTTP = "https://same.ascetic.top/";
export const imgHost = "https://same.ascetic.top/";
import st from "@/utils/showtoast.js";

export let get = (url, data = {}) => {
    return request(url, data, "get");
};
export let post = (url, data = {}) => {
    return request(url, data, "post");
};
let request = (url, data = {}, method) => {
    uni.showLoading({
        title: "请求中",
        mask: true,
    });
    return new Promise((success, fail) => {
        uni.request({
            url: HTTP + url,
            method: method,
            header: {
                token: uni.getStorageSync("token"),
            },
            data: data,
            success: (res) => {
                uni.hideLoading();
                uni.stopPullDownRefresh();
                let data = res.data;
                if (data.code == 1) {
                    success(data);
                } else {
                    fail(data);
                    if (data.code == 401) {
                        uni.$u.toast('请先登录')
                        setTimeout(function () {
                            uni.reLaunch({
                                url: '/liu/pages/login'
                            })
                        }, 1500);
                    }
                }
            },
            fail: (res) => {
                uni.hideLoading();
                uni.stopPullDownRefresh();
                st.show("服务器请求失败，请稍后重试");
                uni.$u.toast('请先登录')
                setTimeout(function () {
                    uni.reLaunch({
                        url: '/liu/pages/login'
                    })
                }, 1500);
            },
        });
    });
};

export let upload = (file) => {
    uni.showLoading({
        title: "上传中",
        mask: true,
    });
    let promise = new Promise((success, fail) => {
        uni.uploadFile({
            filePath: file,
            name: "file",
            url: HTTP + "api/common/upload",
            success(res) {
                uni.hideLoading();
                if (res.errMsg == "uploadFile:ok") {
                    let data = JSON.parse(res.data);
                    if (data.code == 1) {
                        success(data);
                    } else {
                        fail(data);
                    }
                }
            },
            fail: (res) => {
                uni.hideLoading();
            },
        });
    });
    return promise;
};

module.exports = {
    get,
    upload,
    post,
    imgHost,
};
